package com.xm.xm.undercover.utils

import android.util.Log
import com.google.gson.Gson
import com.xm.xm.undercover.bean.GetMessage
import com.xm.xm.undercover.service.RegService
import kotlinx.android.synthetic.main.activity_test.*
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.net.URL
import kotlin.concurrent.thread


object HttpUtils {
    private val JSON = MediaType.parse("application/json; charset=utf-8")

    fun sendRegHttp() {
        val retrofit = Retrofit.Builder()
            .baseUrl("")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        val regService = retrofit.create(RegService::class.java)
    }

    fun forPost(url: String,json:String){
        thread {
            try {
                val body = RequestBody.create(JSON, json)
                val client = OkHttpClient()
                val request = Request.Builder()
                    .url(url)
                    .post(body)
                    .build()
                val response = client.newCall(request).execute()
                Log.d("testActivity", "start reg")
                val responseData = response.body()?.string()
                if (responseData != null) {

                }
            } catch (e: Exception) {
                e.printStackTrace()

                Log.d("testActivity", "FAIL")
            }
        }
    }

    fun toReg(url:String,json:String):GetMessage {
        var message = GetMessage("000",null,null,"nothing")
        val responseData = forPost(url, json)
//        if (responseData.length > 0) {
//            try {
//                val gson = Gson()
//                val message = gson.fromJson<GetMessage>(responseData, GetMessage::class.java)
//                return message
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//        }
        return message
    }
}